<template>
    <div id="companyRegister" class="login_bg login_bg_register">
        <common-login :commonData="{$parent:$parent}"></common-login>
        <div class="login_box company_register_box" style="width: 600px;">
            <ul class="text_center login_box_ul register_box_ul">
                <li>
                    <router-link to="/register">个人注册</router-link>
                </li>
                <li>
                    <router-link class="login_tab_active" to="/company_register">企业注册</router-link>
                </li>
            </ul>
            <form class="login_form" ref="form" :model="form" @keyup.enter="toCompanyRegister(form)">
                <div style="display: flex;justify-content: space-between;">
                    <div style="margin-right: 20px;">
                        <div class="login_input_box">
                            <img class="login_icon" src="/static/img/login/login_company.png">
                            <input type="text" v-model.trim="form.companyName" placeholder="请输入您的企业名称">
                            <a v-if="form.companyName.length>3" class="qichacha" target="_blank" :href="'https://aiqicha.baidu.com/s?q='+form.companyName">爱企查</a>
                        </div>
                        <div class="login_input_box">
                            <img class="login_icon" src="/static/img/login/login_xinyong.png">
                            <input type="text" v-model.trim="form.identification" placeholder="请输入社会信用代码">
                        </div>
                        <div class="login_input_box">
                            <img class="login_icon" src="/static/img/login/login_area.png">
                            <el-select v-model.trim="form.county" placeholder="请选择地区" @change="areaClick" style="width: 270px;">
                                <el-option v-for="(item,index) in areas" :key="index" :label="item.dicKey" :value="item.dicKey">
                                </el-option>
                            </el-select>
                        </div>
                        <div class="login_input_box">
                            <img class="login_icon" src="/static/img/login/login_user.png">
                            <input type="text" v-model.trim="form.telephone" placeholder="请输入绑定手机号">
                        </div>
                        <div class="login_input_box login_input_code">
                            <img class="login_icon" src="/static/img/login/login_code.png">
                            <input type="text" v-model.trim="form.code" placeholder="请输入验证码">
                            <input class="register_getcode" readonly="readonly" :value="verifyCode" :disabled="disabled"
                                @click="getVerifyCode" />
                        </div>
                        <div class="login_input_box">
                            <img class="login_icon" src="/static/img/login/login_pwd.png">
                            <input type="password" v-model.trim="form.pwd" placeholder="请输入您的密码">
                        </div>
                        <div class="login_input_box">
                            <img class="login_icon" src="/static/img/login/login_rePwd.png">
                            <input type="password" v-model.trim="form.rePwd" placeholder="请再次确认您的密码">
                        </div>
                    </div>
                    <div style="width: 250px;text-align: left;margin: 0 auto;margin-top: 20px;">
                        <div style="background:rgba(255,255,255,1);border-radius:4px;margin-bottom: 30px;">
                            <span class="font-size:12px;color:rgba(102,102,102,1);line-height:17px;">上传营业执照</span>
                            <el-upload action='' id="conpamyZhi" drag class="upload-demo" list-type="picture"
                                :before-upload="onBeforeUpload" :on-change="changeBase64" :show-file-list="false">
                                <div v-if="String.isEmptyOrNUll(form.base64)">
                                    <!-- <img src="/static/img/login/identity.png" style="width: 50px;height: 50px;margin-top: 30px;margin-bottom: 11px;" /> -->
                                    <p style="font-size:14px;color:rgba(86,86,86,1);margin-bottom: 4px;margin-top: 120px;">点击或将营业执照图片拖拽到这里上传</p>
                                    <p style="font-size:12px;color:rgba(0,0,0,0.45);">支持jpg和png格式</p>
                                </div>
                                <div v-else>
                                    <img :src="'data:image/png;base64,'+form.base64" class="fill" />
                                </div>
                            </el-upload>
                        </div>
                    </div>
                </div>
                <div class="login_btn_box text_center">
                    <a class="login_btn" @click="toCompanyRegister(form)">立即注册</a>
                    <router-link class="login_a" to="/company_forget">忘记密码</router-link>
                    <span>|</span>
                    <router-link class="login_a" to="/company_login">点击登录</router-link>
                </div>
            </form>
        </div>
    </div>
</template>

<script>
    var _this;
    import commonLogin from '../components/c_login.vue'
    export default {
        name: 'company_forget',
        data() {
            return {
                verifyCode: "获取短信验证码",
                disabled: false,
                areas: [],
                form: {
                    companyName: "",
                    identification: "",
                    county: "", //地区
                    areacode: "",
                    telephone: "",
                    v_token: "",
                    code: "",
                    pwd: "",
                    rePwd: "",
                    base64: ""
                }
            }
        },
        beforeCreate: function() {
            _this = this;
        },
        mounted: function() {
            this.getProCityArea();
        },
        methods: {
            areaClick: function(item) {
                var that = this;
                this.areas.forEach(function(v, i) {
                    if (v.dicKey == item) {
                        that.form.areacode = v.dicValue;
                    }
                });
            },
            getProCityArea: function() {
                var that = this;
                this.common.post(this.api.getCityRepositoryList, {}, function(res) {
                    that.areas = res.resultMap;
                    if (that.areas.length == 1) {
                        that.form.county = that.areas[0].dicKey;
                        that.form.areacode = that.areas[0].dicValue;
                    }
                }, function(error) {
                    that.$message.error(error.resultMsg);
                });
            },
            getVerifyCode: function() {
                var that = this;
                var telephone = this.form.telephone;
                if (telephone == "") {
                    this.$message.error('请输入手机号！');
                    return;
                }
                if (telephone.length != 11) {
                    this.$message.error('请输入正确的手机号！');
                    return;
                }
                this.common.post(this.api.getVerifyCodeRegister, {
                    telephone: telephone
                }, function(data) {
                    that.form.v_token = data.resultMap;
                    that.setInter();
                }, function(error) {
                    that.$message.error(error.resultMsg);
                });
            },
            setInter: function() {
                var that = this;
                var countDown = 60;
                var timer = setInterval(function() {
                    countDown--;
                    that.verifyCode = "短信已发送（" + countDown + "）";
                    that.disabled = true;
                    if (countDown < 1) {
                        clearInterval(timer);
                        that.verifyCode = "获取短信验证码";
                        that.disabled = false;
                    }
                }, 1000);
            },
            onBeforeUpload: function(file) {
                var isIMAGE = ((file.type === 'image/jpeg') || (file.type === 'image/png') || (file.type ===
                    'image/bmp') || (file.type === 'image/jpg'));
                if (!isIMAGE) {
                    this.$message.error('上传文件只能是图片格式（jpeg/png/bmp/jpg）');
                    return false
                }
                if (file.size / 1024 > 200) {
                    this.$alert('图片不能大于200kb', '异常提示', {
                        confirmButtonText: '确定',
                        type: 'warning'
                    });
                    return false
                }
            },
            changeBase64: function(file, fileList) {
                var reader = new FileReader();
                reader.readAsDataURL(file.raw);
                reader.onload = function(e) {
                    var index = this.result.indexOf('base64,'); //base64编码
                    if (index >= 0) {
                        var result = this.result.substring(index + 7);
                        _this.$set(_this.form, "base64", result)
                    }
                }
            },
            toCompanyRegister: function(dataForm) {
                var that = this;
                if (dataForm.base64 == "") {
                    this.$message.error('请上传营业执照！');
                    return;
                }
                if (dataForm.companyName == "") {
                    this.$message.error('请输入企业名称！');
                    return;
                }
                if (dataForm.identification == "") {
                    this.$message.error('请输入社会信用代码！');
                    return;
                }
                if (dataForm.county == "") {
                    this.$message.error('请选择地区！');
                    return;
                }
                if (dataForm.telephone == "") {
                    this.$message.error('请输入手机号！');
                    return;
                }
                if (dataForm.code == "") {
                    this.$message.error('请输入验证码！');
                    return;
                }
                if (dataForm.pwd == "") {
                    this.$message.error('请输入密码！');
                    return;
                }
                if (dataForm.rePwd == "") {
                    this.$message.error('请再次输入密码！');
                    return;
                }
                if (dataForm.pwd != dataForm.rePwd) {
                    this.$message.error('两次密码输入不一致！');
                    return;
                }
                this.common.post(this.api.companyRegister, dataForm, function(data) {
                    that.common.toPage("/company_login");
                }, function(error) {
                    that.$message.error(error.resultMsg);
                });
            }
        },
        components: {
            "common-login": commonLogin
        }
    }
</script>
<style>
    @import url("../assets/css/login.css");

    .login_bg_register {
        height: auto !important;
    }

    .qichacha {
        position: absolute;
        width: 60px;
        right: 0px;
        background: #0C67D0;
        height: 32px;
        line-height: 32px;
        text-align: center;
        border-radius: 4px;
        top: 20px;
        color: #fff;
    }

    #conpamyZhi .el-upload-dragger {
        width: 250px !important;
        height: 320px !important;
    }
</style>
